package com.qst.survey.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.qst.survey.entity.Question;
import com.qst.survey.pojo.PageResult;
import com.qst.survey.pojo.Result;
import com.qst.survey.pojo.StatusCode;
import com.qst.survey.service.QuestionService;
import com.qst.survey.service.UserReplyService;
import com.qst.survey.service.impl.UserReplyServiceImpl;
import com.qst.survey.util.NoAuthorization;
import com.qst.survey.vo.BarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2021/3/18 15:09
 * @Description:
 */
@RestController
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {
    @Autowired
    QuestionService questionService;

    @Autowired
    UserReplyService userReplyService;

    /**
     * @param question: 投票
     * @return com.qst.survey.pojo.Result
     * @description: TODO 投票添加
     * @date 16:40:49 2021/3/18
     */
    @PostMapping("/add")
    public Result addQuestion(Question question, @RequestParam(value = "file", required = false) MultipartFile file) {
        System.out.println(question);
        if (question.getUserId() == null) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }

        String replace = SensitiveWordBs.newInstance().replace(question.getTheme());
        System.out.println(replace);
        question.setTheme(replace);
        String replaceContent = SensitiveWordBs.newInstance().replace(question.getContent());
        System.out.println(replaceContent);
        question.setTheme(replaceContent);

        int i = questionService.addQuestion(question, file);
        if (i == 0) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        } else {
            Map<String, Object> mapX = new HashMap<>();
            mapX.put("question_id", i);
            return new Result(true, StatusCode.OK, "添加成功", mapX);
        }
    }

    /**
     * @param current: 当前页
     * @param size:    每页几行数据
     * @param userId:  用户id
     * @return Result
     * @description: TODO 通过用户id查找试卷
     * @date 13:13:57 2021/3/19
     */
    @GetMapping("/findId/{current}/{size}/{userId}")
    public Result findById(@PathVariable int current, @PathVariable int size, @PathVariable int userId) {
        Page page = new Page(current, size);
        page = questionService.queryByUserId(page, userId);
        PageResult<Question> questionPageResult = new PageResult<Question>(page.getTotal(), page.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", questionPageResult);
    }

    /**p
     * @param current: 当前页
     * @param size:    每页几行数据
     * @param type:    投票类型
     * @return com.qst.survey.pojo.Result
     * @description: TODO 通过类型查找投票(没有结束的投票)
     * @date 13:14:25 2021/3/19
     */
    @NoAuthorization
    @GetMapping("/findType/{current}/{size}/{type}")
    public Result findByType(@PathVariable int current, @PathVariable int size, @PathVariable int type) {
        Page page = new Page(current, size);
        page = questionService.queryByType(page, type);
        PageResult<Question> questionPageResult = new PageResult<Question>(page.getTotal(), page.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", questionPageResult);
    }
    /**
     * @param current: 当前页
     * @param size:    每页几行数据
     * @param type:    投票类型
     * @return com.qst.survey.pojo.Result
     * @description: TODO 通过类型查找投票(没有结束的投票和已经结束的投票)
     * @date 13:14:25 2021/3/19
     */
    @NoAuthorization
    @GetMapping("/findByTypeALL/{current}/{size}/{type}")
    public Result findByTypeALL(@PathVariable int current, @PathVariable int size, @PathVariable int type) {
        Page page = new Page(current, size);
        page = questionService.findByTypeALL(page, type);
        PageResult<Question> questionPageResult = new PageResult<Question>(page.getTotal(), page.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", questionPageResult);
    }


    /**
     * @param current:当前页
     * @param size:       每页几行
     * @return com.qst.survey.pojo.Result
     * @description: TODO 分页查询所有没有结束的投票(不包含内容,只包含投票标题描述封面)
     * @date 13:14:38 2021/3/19
     */
    @NoAuthorization
    @GetMapping("/findAll/{current}/{size}")
    public Result findAll(@PathVariable int current, @PathVariable int size) {
        Page page = new Page(current, size);
        page = questionService.queryAll(page);
        PageResult<Question> questionPageResult = new PageResult<Question>(page.getTotal(), page.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", questionPageResult);
    }

    /**
     * @param current:当前页
     * @param size:       每页几行
     * @return com.qst.survey.pojo.Result
     * @description: TODO 分页查询所有的投票(包含已经结束和没有结束的)
     * @date 13:14:38 2021/3/19
     */
    @NoAuthorization
    @GetMapping("/findAllQu/{current}/{size}")
    public Result findAllQu(@PathVariable int current, @PathVariable int size) {
        Page page = new Page(current, size);
        page = questionService.findAllQu(page);
        PageResult<Question> questionPageResult = new PageResult<Question>(page.getTotal(), page.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", questionPageResult);
    }
    /**
     * @param current: 当前页
     * @param size:    每页几行数据
     * @return com.qst.survey.pojo.Result
     * @description: TODO 分页查询所有模板投票(不包含内容,只包含投票标题描述封面)
     * @date 13:14:38 2021/3/19
     */
    @GetMapping("findAllTemplate/{current}/{size}")
    public Result findAllTemplate(@PathVariable int current, @PathVariable int size) {
        Page page = new Page(current, size);
        page = questionService.findAllTemplate(page);
        PageResult<Question> questionPageResult = new PageResult<>(page.getTotal(), page.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", questionPageResult);
    }

    /**
     * @param current: 当前页
     * @param size:    每页几行数据
     * @return com.qst.survey.pojo.Result
     * @description: TODO 分页查询当前类型的模板投票(不包含内容,只包含投票标题描述封面)
     * @date 13:14:38 2021/3/19
     */
    @GetMapping("findTypeTemplate/{current}/{size}/{type}")
    public Result findTypeTemplate(@PathVariable int current, @PathVariable int size, @PathVariable int type) {
        Page page = new Page(current, size);
        page = questionService.findTypeTemplate(page, type);
        PageResult<Question> questionPageResult = new PageResult<>(page.getTotal(), page.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", questionPageResult);
    }


    /**
     * @param id ： 投票id
     * @return com.qst.survey.pojo.Result
     * @description TODO   通过一个id来查出一个投票对象
     * @date 2021/4/2
     */
    @PostMapping("findQuestionById/{id}")
    public Result findQuestionById(@PathVariable int id) {
        Question question = questionService.findQuestionById(id);
        return new Result(true, StatusCode.OK, "查询成功", question);
    }

    /**
     * @param theme ：投票名
     * @return com.qst.survey.pojo.Result
     * @description TODO   通过投票名字查出投票来
     * @date 2021/4/2
     */
    @NoAuthorization
    @PostMapping("findQuestionByTheme/{theme}")
    public Result findQuestionByTheme(@PathVariable String theme) {
        List<Question> questionList = questionService.findQuestionByTheme(theme);
        return new Result(true, StatusCode.OK, "查询成功", questionList);
    }

    /**
     * @param question ：投票类
     * @return com.qst.survey.pojo.Result
     * @description TODO
     * @date 2021/4/2
     */
    @PostMapping("editQuestion")
    public Result editQuestion(@RequestBody Question question) {
        String replace = SensitiveWordBs.newInstance().replace(question.getTheme());
        System.out.println(replace);
        question.setTheme(replace);
        boolean flag = questionService.editQuestion(question);
        if (flag) {
            return new Result(true, StatusCode.OK, "修改成功", null);
        } else {
            return new Result(false, StatusCode.ERROR, "修改失败", null);
        }
    }


    /**
     * @param id：投票id
     * @return com.qst.survey.pojo.Result
     * @description TODO   通过id查出投票来
     * @date 2021/4/2
     */
    @PostMapping("removeQuestion/{id}")
    public Result removeQuestion(@PathVariable int id) {
        boolean flag = questionService.removeQuestion(id);
        if (flag) {
            return new Result(true, StatusCode.OK, "删除成功", null);
        } else {
            return new Result(false, StatusCode.ERROR, "删除失败", null);
        }
    }

    /**
     * @param questionId ：投票id
     * @return com.qst.survey.pojo.Result
     * @description TODO   通过投票id查出所有问题
     * @date 2021/4/2
     */
    @NoAuthorization
    @GetMapping("/paper/{questionId}")
    public Result completePaper(@PathVariable Integer questionId) {
        Question allTopics = questionService.findAllTopics(questionId);
        System.out.println(allTopics);
        return new Result(true, StatusCode.OK, "查询成功", allTopics);
    }

    /**
     * @param id: 用户id
     * @return com.qst.survey.pojo.Result
     * @description: TODO 通过用户id查询用户所有的投票
     * @date 20:15:25 2021/3/24
     */
    @GetMapping("/questions/{id}")
    public Result queryQuestions(@PathVariable Integer id) {
        List<Question> questions = questionService.queryListById(id);
        return new Result(true, StatusCode.OK, "查询成功", questions);
    }

    /**
     * @param id 投票id
     * @return com.qst.survey.pojo.Result
     * @description TODO 通过投票id删除投票
     * @date 2021/4/2
     */
    @GetMapping("/deleteQuestions/{id}")
    public Result queryDeleteQuestions(@PathVariable Integer id) {
        List<Question> questions = questionService.queryDeleteListById(id);
        return new Result(true, StatusCode.OK, "查询成功", questions);
    }

    /**
     * @param id：投票id
     * @return com.qst.survey.pojo.Result
     * @description TODO 通过投票id删除投票
     * @date 2021/4/2
     */
    @DeleteMapping("/deleteOne/{id}")
    public Result deleteOne(@PathVariable Integer id) {
        Integer integer = questionService.deleteOneQuestion(id);
        if (integer == 1) {
            return new Result(true, StatusCode.OK, "删除成功");
        } else {
            return new Result(false, StatusCode.ERROR, "删除失败");
        }

    }

    /**
     * @param id 投票id
     * @return com.qst.survey.pojo.Result
     * @description TODO 通过投票id从回收站恢复一个投票
     * @date 2021/4/2
     */
    @PostMapping("/recover/{id}")
    public Result recoverQuestion(@PathVariable Integer id) {
        Integer integer = questionService.restoreOneQuestion(id);
        if (integer == 1) {
            return new Result(true, StatusCode.OK, "恢复成功");
        } else {
            return new Result(false, StatusCode.ERROR, "恢复失败");
        }

    }

    /**
     * @param id ：投票id
     * @return com.qst.survey.pojo.Result
     * @description TODO  通过投票id来查出问题的答复
     * @date 2021/4/2
     */
    @GetMapping("/paperData/{id}")
    public Result queryData(@PathVariable Integer id) {
        List allData = questionService.findAllData(id);
        return new Result(true, StatusCode.OK, "查询成功!", allData);
    }

    /**
     * @param id     ：投票id
     * @param userId ：用户id
     * @return com.qst.survey.pojo.Result
     * @description TODO   通过投票和用户的id创建出模板投票来
     * @date 2021/4/2
     */
    @PostMapping("/createTemplate/{id}/{userId}")
    public Result createTemplate(@PathVariable Integer id, @PathVariable Integer userId) {
        int questionId = questionService.createTemplate(id, userId);
        return new Result(true, StatusCode.OK, "复制成功!", questionId);
    }
    /**
     * @return com.qst.survey.pojo.Result
     * @description TODO   把投票设置成模板
     * @date 2021/4/2
     */
    @PostMapping("/setTemplate")
    public Result setTemplate(@RequestBody Question question) {
        boolean flag=questionService.setTemplate(question);
        return new Result(true, StatusCode.OK, "复制成功!", flag);
    }

    /**
     * @return com.qst.survey.pojo.Result
     * @description TODO  得到投票的回答人数，用来统计为折线图
     * @date 2021/4/4
     */
    @GetMapping("/getQueCountLine")
    public Result getQueCountLine() {
        BarVO barVO = questionService.getQueCountLine();
        return new Result(true, StatusCode.OK, "复制成功!", barVO);
    }

    /**
     * 得到投票问答的人数
     *
     * @return
     */
    @NoAuthorization
    @GetMapping("/count")
    public Result count() {
        Integer integer = userReplyService.countReplay();
        return new Result(true, StatusCode.OK, "统计成功", integer);
    }
    /**
     * @return com.qst.survey.pojo.Result
     * @description TODO  通过投票id,来设置用户查看了投票,然后热度加1
     * @date 2021/4/4
     */
    @NoAuthorization
    @GetMapping("/addHeatDegree/{id}")
    public Result addHeatDegree( @PathVariable Integer id){
        boolean flag=questionService.addHeatDegree(id);
        return new Result(flag, StatusCode.OK, "统计成功");
    }
    /**
     * @return com.qst.survey.pojo.Result
     * @description TODO  通过投票id,来判断用户是否已经回复了该投票
     * @date 2021/4/4
     */
    @NoAuthorization
    @GetMapping("/judgeReplay/{id}/{userId}")
    public Result judgeReplay(@PathVariable Integer id,@PathVariable Integer userId){
        if(userId!=0){//userId==0时 判定为没有用户登录
            boolean flag=userReplyService.judgeReplay(id,userId);
            return new Result(flag, StatusCode.OK, "统计成功");
        }else {
            return new Result(false, StatusCode.OK, "统计成功");
        }
    }
}
